package io

import (
	"bytes"
	"io"
	"unicode/utf8"
)

type Buffer struct {
	buf       []byte
	off       int
	runeBytes [utf8.UTFMax]byte
	bootstrap [64]byte
}

func ReadAll(r io.Reader) ([]byte, error) {
	return readAll(r, bytes.MinRead) // const MinRead = 512
}

func readAll(r io.Reader, capacity int64) (b []byte, err error) {
	buf := bytes.NewBuffer(make([]byte, 0, capacity))

	defer func() {
		e := recover()
		if e == nil {
			return
		}
		if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge {
			err = panicErr
		} else {
			panic(e)
		}
	}()

	_, err = buf.ReadFrom(r)
	return buf.Bytes(), err
}
